package com.example.shop.modules.manage.controller;


import com.example.shop.modules.manage.service.IExpressAdminService;
import com.example.shop.modules.manage.service.IOrderAdminService;
import com.example.shop.persistence.entity.Express;
import com.example.shop.persistence.entity.Order;
import com.example.shop.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author jason
 * @since 2019-12-17
 */
@Api(tags = "后台接口订单表")
@RestController
@RequestMapping("/manage/order")
public class OrderAdminController {

    @Autowired
    private IOrderAdminService orderService;

    @Autowired
    private IExpressAdminService expressAdminService;

    @ApiOperation(value = "修改订单")
    @PostMapping("/update")
    public JSONResult updateOrderInfo(Order order){
        return JSONResult.renderSuccess(orderService.updateOrderInfo(order));
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping
    public JSONResult deleteOrderInfo(String ids){
        return JSONResult.renderSuccess( orderService.deleteOrderInfo(ids));
    }

    @ApiOperation(value = "分页查询",notes = "根据状态分页查询")
    @GetMapping("/list")
    public JSONResult getOrderInfoList(Integer page, Integer pageSize, Integer state, Integer id, String phone, String startTime, String endTime){
        return JSONResult.renderSuccess(orderService.selectOrderInfoList(page,pageSize,state,id, phone, startTime, endTime));
    }

    @ApiOperation(value = "查询订单详情")
    @GetMapping("/{id}")
    public JSONResult getOrderInfo(@PathVariable("id") Integer id){
        return orderService.selectOrderInfo(id);
    }

    @ApiOperation(value = "发货(修改)")
    @PostMapping("/updateExpress")
    public JSONResult postExpress(Express express){
        if(express.getId() == null){
            // 新增的时候修改订单状态
            Order order = new Order();
            order.setId(express.getOrderID());
            // 订单状态改为已发货 待确认
            order.setState(1110);
            orderService.updateOrderInfo(order);
        }
        return renderSuccess(expressAdminService.saveOrUpdate(express));
    }

    @ApiOperation(value = "删除运单号")
    @ApiImplicitParam(name = "id", value = "运单ID", required = true, dataType = "Int")
    @PostMapping("/updateExpress/delete")
    public JSONResult deleteExpress(Integer id){
        return renderSuccess(expressAdminService.removeById(id));
    }
}

